home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / genctxt.zip / CHAP9.TXT < prev    next >
Text File  |  1987-11-21  |  32KB  |  719 lines

  1.  
  2.                      Chapter 9 - Standard Input/Output
  3.  
  4.  
  5.                           THE STDIO.H HEADER FILE
  6.  
  7.              Load  the file SIMPLEIO.C for our first look at a  file
  8.         with  standard I/O.   Standard I/O refers to the most  usual
  9.         places  where  data is either read from,  the  keyboard,  or
  10.         written to, the video monitor.  Since they are used so much,
  11.         they are used as the default I/O devices and do not need  to
  12.         be  named in the Input/Output instructions.   This will make
  13.         more  sense when we actually start to use them so lets  look
  14.         at the file in front of you.
  15.  
  16.              The  first thing you will notice is the second line  of
  17.         the  file,  the #include "stdio.h" line.   This is very much
  18.         like  the  #define  we have  already  studied,  except  that
  19.         instead of a simple substitution,  an entire file is read in
  20.         at  this  point.   The  system  will  find  the  file  named
  21.         "stdio.h"  and read its entire contents in,  replacing  this
  22.         statement.   Obviously then,  the file named "stdio.h"  must
  23.         contain  valid  C source statements that can be compiled  as
  24.         part  of  a program.   This particular file is  composed  of
  25.         several standard #defines to define some of the standard I/O
  26.         operations.   The file is called a header file and you  will
  27.         find several different header files on the source disks that
  28.         came  with your C compiler.  Each of the header files has  a
  29.         specific  purpose and any or all of them can be included  in
  30.         any program.
  31.  
  32.              Your C compiler uses the double quote marks to indicate
  33.         that  the  search for the "include" file will begin  in  the
  34.         current  directory,  and if it not found there,  the  search
  35.         will  continue in the "include" directory as set up  in  the
  36.         environment.   It  also uses the "less  than"  and  "greater
  37.         than" signs to indicate that the file search should begin in
  38.         the  directory  specified in the environment.  Most  of  the
  39.         programs  in  this tutorial have the double  quotes  in  the
  40.         "include" statements.  The next program uses the "<" and ">"
  41.         to  illustrate the usage.  Note that this will result  is  a
  42.         slightly  faster  (but  probably  unnoticeable)  compilation
  43.         because  the  system will not bother to search  the  current
  44.         directory.
  45.  
  46.                         INPUT/OUTPUT OPERATIONS IN C
  47.  
  48.              Actually  the  C programming language has no  input  or
  49.         output operations defined as part of the language, they must
  50.         be user defined.   Since everybody does not want to reinvent
  51.         his  own input and output operations,  the compiler  writers
  52.         have done a lot of this for us and supplied us with  several
  53.         input  functions and several output functions to aid in  our
  54.         program development.   The functions have become a standard,
  55.         and  you  will find the same functions available  in  nearly
  56.  
  57.  
  58.                                   Page 59
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.                      Chapter 9 - Standard Input/Output
  69.  
  70.  
  71.         every  compiler.   In fact,  the industry standard of the  C
  72.         language  definition has become the book written by Kernigan
  73.         and Ritchie, and they have included these functions in their
  74.         definition.   You will often,  when reading literature about
  75.         C, find a reference to K & R.  This refers to the book, "The
  76.         C  Programming Language", written by Kernigan  and  Ritchie.
  77.         You would be advised to purchase a copy for reference.
  78.  
  79.              You should print out the file named "stdio.h" and spend
  80.         some  time studying it.   There will be a lot that you  will
  81.         not understand about it, but parts of it will look familiar.
  82.         The  name  "stdio.h"  is  sort  of  cryptic  for   "standard
  83.         input/output header",  because that is exactly what it does.
  84.         It  defines  the standard input and output functions in  the
  85.         form of #defines and macros.  Don't worry too much about the
  86.         details  of this now.   You can always return to this  topic
  87.         later  for  more study if it interests  you,  but  you  will
  88.         really  have no need to completely understand the  "stdio.h"
  89.         file.  You will have a tremendous need to use it however, so
  90.         these comments on its use and purpose are necessary.
  91.  
  92.                             OTHER INCLUDE FILES
  93.  
  94.              When  you  begin writing larger programs and  splitting
  95.         them  up into separately compiled portions,  you  will  have
  96.         occasion  to  use  some  statements common to  each  of  the
  97.         portions.   It would be to your advantage to make a separate
  98.         file  containing  the  statements and use  the  #include  to
  99.         insert it into each of the files.  If you want to change any
  100.         of the common statements,  you will only need to change  one
  101.         file  and  you will be assured of having all of  the  common
  102.         statements  agree.   This  is  getting  a  little  ahead  of
  103.         ourselves  but  you  now  have  an  idea  how  the  #include
  104.         directive can be used.
  105.  
  106.                     BACK TO THE FILE NAMED "SIMPLEIO.C"
  107.  
  108.              Lets  continue our tour of the file in  question.   The
  109.         one  variable  "c" is defined and a message is  printed  out
  110.         with the familiar "printf" function.  We then find ourselves
  111.         in  a continuous loop as long as "c" is not equal to capital
  112.         X.   If  there is any question in your mind about  the  loop
  113.         control, you should review chapter 3 before continuing.  The
  114.         two  new functions within the loop are of paramount interest
  115.         in this program since they are the new functions.  These are
  116.         functions to read a character from the keyboard and  display
  117.         it on the monitor one character at a time.
  118.  
  119.              The  function "getchar()" reads a single character from
  120.         the  standard  input  device,  the  keyboard  being  assumed
  121.         because that is the standard input device, and assigns it to
  122.  
  123.  
  124.                                   Page 60
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.                      Chapter 9 - Standard Input/Output
  135.  
  136.  
  137.         the variable "c".   The next function "putchar(c)", uses the
  138.         standard output device,  the video monitor,  and outputs the
  139.         character contained in the variable "c".   The character  is
  140.         output  at  the  current cursor location and the  cursor  is
  141.         advanced  one space for the next character.   The system  is
  142.         therefore taking care of a lot of the overhead for us.   The
  143.         loop  continues reading and displaying characters  until  we
  144.         type a capital X which terminates the loop.
  145.  
  146.              Compile and run this program for a few surprises.  When
  147.         you type on the keyboard, you will notice that what you type
  148.         is displayed faithfully on the screen,  and when you hit the
  149.         return key,  the entire line is repeated.   In fact, we only
  150.         told  it  to output each character once but it seems  to  be
  151.         saving  the  characters up and redisplaying them.   A  short
  152.         explanation is in order.
  153.  
  154.                DOS IS HELPING US OUT (OR GETTING IN THE WAY)
  155.  
  156.              We need to understand a little bit about how DOS  works
  157.         to  understand  what is happening here.   When data is  read
  158.         from  the keyboard,  under DOS control,  the characters  are
  159.         stored  in  a buffer until a carriage return is  entered  at
  160.         which  time the entire string of characters is given to  the
  161.         program.   When the characters are being typed, however, the
  162.         characters are displayed one at a time on the monitor.  This
  163.         is called echo,  and happens in many of the applications you
  164.         run.
  165.  
  166.              With  the above paragraph in mind,  it should be  clear
  167.         that when you are typing a line of dat